package com.ruoyi.business.Schedul;

import com.ruoyi.business.domain.Coupon;
import com.ruoyi.business.service.impl.CouponServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * @author ffc
 * @version 1.0
 * @description: TODO
 * @className: CouponSchedul
 * @date 2024/9/18 14:31
 */

@Component
public class CouponSchedul {
    @Autowired
    private CouponServiceImpl couponService;
    @Autowired
    private RedisTemplate redisTemplate;
    //更新优惠券的数量
    @Scheduled(cron = "*/6 * * * * ?")
    private void updateCoupon(){
        Set coupon = redisTemplate.opsForHash().keys("coupon");
        for (Object item : coupon) {
            String s = item.toString();
            Object coupon1 = redisTemplate.opsForHash().get("coupon", s);
            Long num = Long.parseLong(coupon1.toString());
            Coupon coupon2 = new Coupon();
            coupon2.setId(Long.valueOf(s));
            coupon2.setNum(num);
            couponService.updateCoupon(coupon2);
        }
    }
}
